// CSE 142 Winter 2008, Marty Stepp // // A Snake slithers by moving in the following way: // 1 east, 1 south, 2 west, 1 south, 3 east, 1 south, 4 west, 1 south, 5 west, ... import java.awt.*; // for Color import java.util.*; // for Random public class Snake extends Critter { // fields private int horizontal; // how wide of a horizontal cycle I'm on right now private int distanceTraveled; // how much of this cycle have I completed? private Direction cycleDirection; // which way is the current cycle? // constructor // This initializes the state of a new Snake object as it is created. public Snake() { horizontal = 1; distanceTraveled = 0; cycleDirection = Direction.EAST; } public Direction getMove() { // for debugging // System.out.println("Snake move: horiz=" + horizontal + ", dist=" + // distanceTraveled + ", cycleDir=" + cycleDirection); if (distanceTraveled < horizontal) { // a move in the middle of a cycle distanceTraveled++; return cycleDirection; } else { // we have just reached the end of the cycle horizontal++; distanceTraveled = 0; if (cycleDirection == Direction.EAST) { cycleDirection = Direction.WEST; } else { cycleDirection = Direction.EAST; } return Direction.SOUTH; } } public Attack fight(String opponent) { Random randy = new Random(); int choice = randy.nextInt(2); if (choice == 0) { return Attack.ROAR; } else { return Attack.POUNCE; } } public Color getColor() { // red=64, green=228, blue=32 return new Color(64, 228, 32); } public String toString() { return "S"; } // I don't need an eat method since the default is to return false anyway // public boolean eat() { // return false; // } }